<template>
  <view class="settings-container">
    <!-- 页面标题 -->
    <view class="settings-header" :style="{ paddingTop: `${statusBarHeight + 40}rpx` }">
      <text class="page-title" @click="handleTitleClick">设置</text>
      <text class="page-subtitle">个性化您的AI助手</text>
    </view>
    
    <!-- 设置内容 - 使用view代替scroll-view -->
    <view class="settings-content">
      <!-- 个性化设置 -->
      <view class="settings-section">
        <text class="section-title">个性化</text>
        
        <view class="settings-item" @click="goToModelSelect" hover-class="item-hover">
          <view class="item-left">
            <my-icon name="user" :size="22" color="var(--primary-color)" />
            <text class="item-title">模型选择</text>
          </view>
          <view class="item-right">
            <text class="item-value">{{ currentModel }}</text>
            <text class="item-arrow">›</text>
          </view>
        </view>
        
        <view class="settings-item" @click="showSummaryThresholdPicker" hover-class="item-hover">
          <view class="item-left">
            <my-icon name="file" :size="22" color="var(--primary-color)" />
            <view class="item-info">
              <text class="item-title">对话总结阈值</text>
              <text class="item-desc">对话达到设定轮数后会自动生成总结</text>
            </view>
          </view>
          <view class="item-right">
            <text class="item-value">{{ settingsManager.summaryThreshold }}轮</text>
            <text class="item-arrow">›</text>
          </view>
        </view>
      </view>
      
      <!-- 内容设置 -->
      <view class="settings-section" v-if="showAdultSettings">
        <text class="section-title">内容设置</text>
        
        <view class="settings-item">
          <view class="item-left">
            <my-icon name="error-circle" :size="22" color="var(--primary-color)" />
            <text class="item-title">显示高级角色</text>
          </view>
          <view class="item-right">
            <switch 
              :checked="settingsManager.showAdultContent" 
              @change="toggleAdultContent" 
              color="var(--primary-color)"
              style="transform: scale(0.8);"
            />
          </view>
        </view>

        <view class="settings-item" @click="goPicShow" hover-class="item-hover">
          <view class="item-left">
            <my-icon name="image" :size="22" color="var(--primary-color)" />
            <view class="item-info">
              <text class="item-title">看图模式</text>
              <text class="item-desc">浏览高级图片内容</text>
            </view>
          </view>
          <view class="item-right">
            <text class="item-arrow">›</text>
          </view>
        </view>
		<view class="settings-item" @click="goPicShow2" hover-class="item-hover">
		  <view class="item-left">
		    <my-icon name="image" :size="22" color="var(--primary-color)" />
		    <view class="item-info">
		      <text class="item-title">生图模式</text>
		      <text class="item-desc">生成高级图片内容</text>
		    </view>
		  </view>
		  <view class="item-right">
		    <text class="item-arrow">›</text>
		  </view>
		</view>

        <view class="settings-item">
          <view class="item-left">
            <my-icon name="eye" :size="22" color="var(--primary-color)" />
            <view class="item-info">
              <text class="item-title">沉浸模式</text>
              <text class="item-desc">隐藏界面元素，专注于对话</text>
            </view>
          </view>
          <view class="item-right">
            <switch 
              :checked="settingsManager.immersiveMode" 
              @change="toggleImmersiveMode" 
              color="var(--primary-color)"
              style="transform: scale(0.8);"
            />
          </view>
        </view>
      </view>
      
      <!-- 其他设置 -->
      <view class="settings-section">
        <text class="section-title">其他</text>
        
        <view class="settings-item" @click="goToUserLogin" hover-class="item-hover">
          <view class="item-left">
            <my-icon name="user" :size="22" color="var(--primary-color)" />
            <text class="item-title">{{ isLoggedIn ? '用户信息' : '用户登录/注册' }}</text>
          </view>
          <view class="item-right">
            <text class="item-value">{{ isLoggedIn ? '已登录' : '未登录' }}</text>
            <text class="item-arrow">›</text>
          </view>
        </view>
        
        <view class="settings-item" v-if="isLoggedIn" @click="handleLogout" hover-class="item-hover">
          <view class="item-left">
            <my-icon name="exit" :size="22" color="#e74c3c" />
            <text class="item-title">退出登录</text>
          </view>
          <view class="item-right">
            <text class="item-arrow">›</text>
          </view>
        </view>
        
        <view class="settings-item" @click="showAbout" hover-class="item-hover">
          <view class="item-left">
            <my-icon name="info" :size="22" color="var(--primary-color)" />
            <text class="item-title">关于</text>
          </view>
          <view class="item-right">
            <text class="item-value">v{{ appVersion }}</text>
            <text class="item-arrow">›</text>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 对话总结阈值设置模态框 -->
    <view class="modal" v-if="showSummaryModal" @click.stop="closeSummaryModal">
      <view class="modal-mask"></view>
      <view class="modal-wrapper" @click.stop>
        <view class="modal-content">
          <view class="modal-header">
            <text class="modal-title">设置对话总结阈值</text>
            <view class="modal-close" @click="closeSummaryModal">
              <my-icon name="close" :size="20" color="var(--text-secondary)" />
            </view>
          </view>
          
          <view class="modal-body">
            <view class="threshold-display">
              <text class="threshold-value">{{ tempSummaryThreshold }}轮</text>
              <text class="threshold-desc">当对话达到此轮数后将自动生成总结</text>
            </view>
            
            <view class="quick-select">
              <view 
                v-for="value in presetThresholds" 
                :key="value"
                class="quick-btn" 
                :class="{ active: tempSummaryThreshold === value }"
                hover-class="btn-hover"
                @click="handleSummaryThreshold(value)"
              >
                {{ value }}轮
              </view>
            </view>
            
            <view class="custom-input">
              <text class="input-label">自定义轮数</text>
              <input
                type="number"
                v-model="tempSummaryThreshold"
                class="input-field"
                placeholder="输入5-1000的数字"
                @input="handleCustomInput"
              />
            </view>
          </view>
          
          <view class="modal-footer">
            <button 
              class="modal-btn cancel" 
              hover-class="btn-hover"
              @click="closeSummaryModal"
            >取消</button>
            <button 
              class="modal-btn confirm" 
              hover-class="btn-hover"
              @click="applySummaryThreshold"
            >确定</button>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import modelManager from '@/store/model.js'
import settingsManager from '@/store/settings.js'

export default {
  data() {
    return {
      statusBarHeight: 0,
      currentModel: '',
      showAdultSettings: false,
      settingsManager,
      appVersion: '1.0.0',
      titleClickCount: 0,
      titleClickTimer: null,
      showSummaryModal: false,
      tempSummaryThreshold: 10,
      presetThresholds: [5, 10, 20, 50, 100],
      isLoggedIn: false
    }
  },
  
  onLoad() {
    this.initializeSettings();
    uni.$on('settingsChange', this.handleSettingsChange);
  },
  
  onUnload() {
    uni.$off('settingsChange', this.handleSettingsChange);
  },
  
  onShow() {
    this.syncSettings();
  },
  
  methods: {
    // 初始化设置
    initializeSettings() {
      this.getStatusBarHeight();
      this.currentModel = this.getCurrentModelName();
      this.showAdultSettings = settingsManager.adultSettingUnlocked;
      this.isLoggedIn = settingsManager.isLoggedIn;
    },
    
    // 同步设置状态
    syncSettings() {
      this.currentModel = this.getCurrentModelName();
      this.showAdultSettings = settingsManager.adultSettingUnlocked;
      this.isLoggedIn = settingsManager.isLoggedIn;
    },
    
    // 获取当前模型名称
    getCurrentModelName() {
      const currentModelId = modelManager.currentModel;
      const model = modelManager.models.find(m => m.id === currentModelId);
      return model ? model.name : '未知模型';
    },
    
    // 获取状态栏高度
    getStatusBarHeight() {
      try {
        const systemInfo = uni.getSystemInfoSync();
        this.statusBarHeight = systemInfo.statusBarHeight || 0;
        
        // #ifdef H5
        if (systemInfo.platform === 'web') {
          this.statusBarHeight = 0;
        }
        // #endif
      } catch (e) {
        console.error('获取状态栏高度失败:', e);
        this.statusBarHeight = 0;
      }
    },
    
    // 处理设置变更
    handleSettingsChange(data) {
      if (data.type === 'adultSettingUnlocked') {
        this.showAdultSettings = data.value;
      } else if (data.type === 'immersiveMode') {
        // 处理沉浸模式变更
        this.handleImmersiveModeChange(data.value);
      }
    },
    
    // 处理标题点击
    handleTitleClick() {
      if (this.titleClickTimer) {
        clearTimeout(this.titleClickTimer);
      }
      
      this.titleClickCount++;
      
      this.titleClickTimer = setTimeout(() => {
        this.titleClickCount = 0;
      }, 3000);
      
      if (this.titleClickCount >= 10) {
        this.unlockAdultSettings();
      } else if (this.titleClickCount === 5 || this.titleClickCount === 8) {
        uni.vibrateShort();
        uni.showToast({
          title: `再点击${10 - this.titleClickCount}次解锁高级模式`,
          icon: 'none',
          duration: 1000
        });
      }
    },
    
    // 解锁成人设置
    unlockAdultSettings() {
      settingsManager.adultSettingUnlocked = true;
      this.showAdultSettings = true;
      
      uni.vibrateShort();
      uni.showToast({
        title: '高级模式已开启',
        icon: 'success',
        duration: 1500
      });
      
      this.titleClickCount = 0;
      if (this.titleClickTimer) {
        clearTimeout(this.titleClickTimer);
      }
    },
    
    // 跳转到模型选择
    goToModelSelect() {
      uni.navigateTo({
        url: '/pages/model/select'
      });
    },
    
    // 切换成人内容
    toggleAdultContent(e) {
      const showAdult = e.detail.value;
      settingsManager.showAdultContent = showAdult;
      uni.$emit('settingsChange', { showAdultContent: showAdult });
    },
    
    // 显示关于信息
    showAbout() {
      uni.showModal({
        title: '关于应用',
        content: `版本: ${this.appVersion}\n\n感谢您使用我们的应用！`,
        showCancel: false,
        confirmText: '确定'
      });
    },
    
    // 显示对话总结阈值选择器
    showSummaryThresholdPicker() {
      this.tempSummaryThreshold = this.settingsManager.summaryThreshold;
      this.showSummaryModal = true;
    },
    
    // 关闭模态框
    closeSummaryModal() {
      this.showSummaryModal = false;
    },
    
    // 处理对话总结阈值选择
    handleSummaryThreshold(value) {
      this.tempSummaryThreshold = parseInt(value);
    },
    
    // 处理自定义输入
    handleCustomInput(e) {
      let value = parseInt(e.detail.value);
      
      if (isNaN(value) || value < 5) {
        value = 5;
      } else if (value > 1000) {
        value = 1000;
      }
      
      this.tempSummaryThreshold = value;
    },
    
    // 应用设置
    applySummaryThreshold() {
      this.settingsManager.summaryThreshold = this.tempSummaryThreshold;
      
      uni.showToast({
        title: `对话总结阈值已设为${this.tempSummaryThreshold}轮`,
        icon: 'success',
        duration: 1500
      });
      
      this.closeSummaryModal();
    },
    
    // 跳转到用户登录/注册页面
    goToUserLogin() {
      uni.navigateTo({
        url: '/pages/user/login'
      });
    },
    
    // 处理退出登录
    handleLogout() {
      settingsManager.logout();
      this.isLoggedIn = false;
      
      uni.showToast({
        title: '已退出登录',
        icon: 'success',
        duration: 1500
      });
    },
    
    // 切换沉浸模式
    toggleImmersiveMode(e) {
      const isImmersive = e.detail.value;
      settingsManager.immersiveMode = isImmersive;
    },
    
    // 处理沉浸模式变更
    handleImmersiveModeChange(isImmersive) {
      // 显示提示
      uni.showToast({
        title: isImmersive ? '已开启沉浸模式' : '已关闭沉浸模式',
        icon: 'none',
        duration: 1500
      });
    },
    
    // 跳转到看图模式
    goPicShow() {
      if (!this.settingsManager.showAdultContent) {
        uni.showModal({
          title: '权限提示',
          content: '请先开启高级角色显示功能',
          showCancel: false,
          confirmText: '我知道了'
        });
        return;
      }
      
      uni.navigateTo({
        url: '/pages/test/picshow'
      });
    },
	// 跳转到看图模式
	goPicShow2() {
	  if (!this.settingsManager.showAdultContent) {
	    uni.showModal({
	      title: '权限提示',
	      content: '请先开启高级角色显示功能',
	      showCancel: false,
	      confirmText: '我知道了'
	    });
	    return;
	  }
	  
	  uni.navigateTo({
	    url: '/pages/test/test'
	  });
	}
  }
}
</script>

<style lang="scss">
.settings-container {
  min-height: 100vh;
  background-color: #121212;
  color: var(--text-color);
  padding: 0 40rpx;
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100vh;
  box-sizing: border-box;
  overflow-y: auto; /* 添加垂直滚动支持 */
}

.settings-header {
  margin-bottom: 60rpx;
  flex-shrink: 0;
  
  .page-title {
    font-size: 48rpx;
    font-weight: bold;
    margin-bottom: 20rpx;
    display: block;
  }
  
  .page-subtitle {
    font-size: 28rpx;
    color: var(--text-secondary);
  }
}

.settings-content {
  display: flex;
  flex-direction: column;
  flex: 1;
  padding-bottom: 40rpx; /* 底部留出足够的空间 */
}

.settings-section {
  margin-bottom: 60rpx;
  
  .section-title {
    font-size: 24rpx;
    color: var(--text-tertiary);
    margin-bottom: 20rpx;
    padding-left: 20rpx;
    text-transform: uppercase;
  }
}

.settings-item {
  background-color: var(--card-bg);
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  .item-left {
    display: flex;
    align-items: center;
    gap: 20rpx;
    
    .item-info {
      display: flex;
      flex-direction: column;
    }
    
    .item-title {
      font-size: 32rpx;
      color: var(--text-color);
    }
    
    .item-desc {
      font-size: 24rpx;
      color: var(--text-secondary);
      margin-top: 6rpx;
    }
  }
  
  .item-right {
    display: flex;
    align-items: center;
    
    .item-value {
      font-size: 28rpx;
      color: var(--text-secondary);
      margin-right: 10rpx;
    }
    
    .item-arrow {
      font-size: 36rpx;
      color: var(--text-secondary);
    }
  }
}

.item-hover {
  background-color: rgba(255, 255, 255, 0.15);
}

/* 模态框样式 */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  display: flex;
  align-items: flex-end;
}

.modal-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.7);
}

.modal-wrapper {
  position: relative;
  width: 100%;
  max-height: 90vh;
  z-index: 1;
  animation: slideUp 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.modal-content {
  background-color: var(--modal-bg);
  border-radius: 24rpx 24rpx 0 0;
  overflow: hidden;
}

@keyframes slideUp {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

.modal-header {
  padding: 30rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  border-bottom: 1px solid var(--border-color);
}

.modal-title {
  font-size: 32rpx;
  font-weight: bold;
  color: var(--text-color);
}

.modal-close {
  position: absolute;
  right: 30rpx;
  top: 50%;
  transform: translateY(-50%);
  width: 40rpx;
  height: 40rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-body {
  padding: 40rpx 30rpx;
}

.threshold-display {
  text-align: center;
  margin-bottom: 40rpx;
}

.threshold-value {
  font-size: 60rpx;
  font-weight: 700;
  color: var(--primary-color);
  display: block;
  margin-bottom: 10rpx;
}

.threshold-desc {
  font-size: 28rpx;
  color: var(--text-secondary);
}

.quick-select {
  display: flex;
  flex-wrap: wrap;
  gap: 20rpx;
  margin: 30rpx 0;
  justify-content: center;
}

.quick-btn {
  padding: 15rpx 30rpx;
  border-radius: 40rpx;
  background-color: var(--card-bg);
  color: var(--text-color);
  font-size: 28rpx;
}

.quick-btn.active {
  background-color: var(--primary-color);
  color: var(--text-color);
}

.btn-hover {
  opacity: 0.8;
}

.custom-input {
  display: flex;
  align-items: center;
  margin: 30rpx 0 0;
  background-color: var(--card-bg);
  border-radius: 16rpx;
  padding: 20rpx 30rpx;
}

.input-label {
  font-size: 28rpx;
  color: var(--text-color);
  margin-right: 20rpx;
}

.input-field {
  flex: 1;
  height: 60rpx;
  border: none;
  background: transparent;
  font-size: 28rpx;
  color: var(--text-color);
}

.modal-footer {
  display: flex;
  border-top: 1px solid var(--border-color);
}

.modal-btn {
  flex: 1;
  padding: 30rpx;
  text-align: center;
  font-size: 32rpx;
  background: transparent;
  border: none;
}

.modal-btn.cancel {
  color: var(--text-secondary);
  border-right: 1px solid var(--border-color);
}

.modal-btn.confirm {
  color: var(--primary-color);
  font-weight: bold;
}
</style>